<template>
  <NewCard title="收银台" isright="isright">
    <template slot="right">
      <span class="hintRight">请在 <DownTime :time="timestamp"></DownTime> 内完成支付，超时自动关闭订单</span>
    </template>
    <div class="pay">
      <div class="pay_main">
        <h3>订单信息</h3>
        <div class="info-list">
          <span>订单编号</span>
          <p>{{ orderinfo.orderNo }}</p>
        </div>
        <div class="info-list">
          <span>订单类型</span>
          <p>项目</p>
        </div>
        <div class="info-list">
          <span>订单名称</span>
          <p>{{ orderinfo.title }}</p>
        </div>
        <div class="info-list">
          <span>委托地区</span>
          <p>{{ orderinfo.goods.city }}</p>
        </div>
        <div class="info-list">
          <span>创建时间</span>
          <p>{{ orderinfo.createdTime }}</p>
        </div>
        <div class="info-list">
          <span>订单金额</span>
          <p>{{ orderinfo.goods.amount | sumFilters }}</p>
        </div>
        <div class="info-list">
          <span>已支付</span>
          <p>{{ getPaid() | sumFilters }}</p>
        </div>
        <div class="info-list">
          <span>本次支付</span>
          <p>{{ getNowamount() | sumFilters }}</p>
        </div>

        <el-row>
          <el-col :span="24">
            <Compay ref="compay" :orderinfo="orderinfo" :nextFlowId="nextFlowId" :acceptEmail="$store.state.user.email"></Compay>
          </el-col>
        </el-row>
      </div>
      <div class="payBtn" v-if="payBtnShow">
        <p>
          应付问金：<span>{{ getNowamount() | sumFilters }}</span>
        </p>
        <el-button round @click="payBtn">立即支付</el-button>
      </div>
    </div>
  </NewCard>
</template>

<script>
import NewCard from "@/wss/components/newCard.vue";
import Compay from "@/BMViews/components/Compay/index.vue";
import { getDepositVoucherPayApi } from "@/BMViews/api/financial/deposit.js";
import { editProjectApi } from "@/BMViews/api/doc/project.js";
import DownTime from "@/components/downTime/index.vue"
export default {
  name: "Pay",
  components: { NewCard, Compay, DownTime },
  props: {
    orderinfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    flow: {
      type: Object,
    },
  },
  data() {
    return {
      isright: true, //显示时间提示支付
      email: "", //电邮
      acceptEmail: "", //接收邮箱
      activeName: "askGold", //默认显示问金支付
      circleUrl:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png", //头像
      askRadio: "2",

      minutes: 15, //分
      seconds: 0, //，秒
      //订单信息
      order: {
        id: "",
        amount: 0,
        flowId: "",
      },
      nextFlowId:'', //第二步的id

      payBtnShow: true,// 当前底部支付按钮的显示隐藏
      timestamp: 0,//时间戳
    };
  },
  mounted() {
    // 获取订单的id
    // this.orderinfo.id = this.orderinfo.id
    // 获取项目当前步骤的id
    this.getFlowId() //获取当前需要支付的flowId
    // console.log(this.order,'-------------------');
    
    // this.countdown(); //倒计时
    // 获取时间戳
    let nowDate = new Date()
    let date = new Date(this.orderinfo.createdTime)
    this.timestamp = date.getTime() + 60*1000*15 - nowDate.getTime()

    // 通过$bus的扫码支付成功后执行的函数
    this.$bus.$on("EventPaymentCompletion", () => {
      // 支付成功后跳转到我的主页-我的已购当前项目的详情
      this.$router.push("/Bm-personalCenter/homepage");
    });

    // 当切换到二维码支付的时候要隐藏底部的按钮
    this.$bus.$on('hidePayBtn',(name)=>{
      console.log(name,'---------------------');
      if(name == 'scanQr'){
          this.payBtnShow = false
      }else{
          this.payBtnShow = true
      }
    })
  },
  methods: {
    // 获取当前需要支付的flowId
    getFlowId(){
      let flows = this.orderinfo.goods.flows;
      //根据no字段排序
      let arr = flows.sort((a, b) => Number(a.no)- Number(b.no))
      for (let i = 0; i < arr.length; i++) {
        if (!arr[i].isPaid) {
          // 获取订单的第一步id
          this.order.flowId = arr[i].id;
          // 获取订单的第二步id
          this.nextFlowId = arr[i+1].id;
          return;
        }
      }
    },

    // 计算已支付的金额
    getPaid() {
      let flows = this.orderinfo.goods.flows;
      let paidArr = flows.filter((item) => {
        return item.isPaid == true;
      });
      let amount = 0;
      paidArr.forEach((item) => {
        amount = amount + (item.scale / 100) * this.orderinfo.goods.amount;
      });
      return amount;
    },

    // 计算本次支付的金额
    getNowamount(){
      let flows = this.orderinfo.goods.flows;
      let paidArr = flows.find((item) => {
        return item.isPaid == false;
      });
      let amount = 0;
      if(paidArr){
        amount = amount + (paidArr.scale / 100) * this.orderinfo.goods.amount;
      }
      return amount;
    },

    // *******************电邮*******************
    // 邮箱的验证
    rulEmail(email) {
      let reg = /^[A-Za-z0-9_\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (email == "") {
        this.$message({ type: "warning", message: "邮箱不能为空！" });
      } else {
        if (!reg.test(email)) {
          this.$message({ type: "warning", message: "请输入有效的邮箱！" });
        }
      }
    },

    // 问金支付
    payBtn() {
      this.askGoldPay(); //支付回调
    },

    // 支付回调
    async askGoldPay() {
      let circleId = this.$refs.compay.getCirecleDefaultPay();
      let { code, msg } = await getDepositVoucherPayApi({
        circleId,
        orderId: this.orderinfo.id,
        flowId: this.order.flowId,
        nextFlowId: this.nextFlowId,
      });
      if (code != 200) return;
      this.$message({ message: "支付成功！", type: "success" });

      // 支付成功后刷新列表 并显示推荐页面
      this.$bus.$emit("skipFeat");
    },

    // 项目修改的接口
    async editProject(newArr) {
      let data = {
        id: this.orderinfo.goodsId,
        flows: newArr,
      };
      let result = await editProjectApi(data);
      if (result.code == 200) {
        // this.$message({ type: "success", message: "保存成功！" });
      }
    },

    //倒计时
    num(n) {
      return n < 10 ? "0" + n : "" + n;
    },
    countdown() {
      var _this = this;
      var time = window.setInterval(function () {
        if (_this.seconds === 0 && _this.minutes !== 0) {
          _this.seconds = 59;
          _this.minutes -= 1;
        } else if (_this.minutes === 0 && _this.seconds === 0) {
          _this.seconds = 0;
          window.clearInterval(time);
          _this.changes();
        } else {
          _this.seconds -= 1;
        }
      }, 1000);
    },

    //返回推荐页面
    changes() {
      // this.$bus.$emit('featHandlerShow', 1)
    },
  },

  computed: {
    //计算时间
    second() {
      return this.num(this.seconds);
    },
    minute() {
      return this.num(this.minutes);
    },
  },

  watch: {
    //监听时间
    second: {
      handler(newVal) {
        this.num(newVal);
      },
    },
    minute: {
      handler(newVal) {
        this.num(newVal);
      },
    },

    // 监听数据变化
    orderinfo: {
      handler(newVal) {
        let flows = this.orderinfo.goods.flows;
        for (let i = 0; i < flows.length; i++) {
          if (!flows[i].isPaid) {
            this.order.flowId = flows[i].id;
            return;
          }
        }
      },
      deep: true, //开启深度监听
    },
  },
};
</script>

<style scoped>
.hintRight {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #999;
}
::v-deep .el-card__body {
  position: relative;
}
/* 支付信息 */
.pay {
  margin-top: 15px;
}
.pay_main {
  padding: 0 30px;
  margin-bottom: 70px;
}

.pay_main .info-list {
  width: 100%;
  display: flex;
  margin-bottom: 15px;
}
.pay_main .info-list > span {
  width: 100px;
  color: #999;
}
.pay_main .info-list > p {
  flex: 1;
  color: #333;
}

.pay_main .el-card {
  border-radius: 12px;
}
::v-deep .pay_main .el-card__body {
  padding: 20px !important;
}
.pay_main h3 {
  margin: 0;
  margin-bottom: 20px;
  font-size: 16px;
  color: #39486f;
  font-weight: bold;
}
.pay_main p {
  font-size: 14px;
  color: #999;
  margin: 0;
  line-height: 24px;
}
.pay_main .el-row {
  margin-top: 40px;
}
::v-deep .pay_main .el-row .el-input__inner {
  border-radius: 8px;
  height: 44px;
  line-height: 44px;
}
.pay_main .el-row .acceptEmail {
  border: 0;
  padding: 5px 0;
  margin: 7px 0;
  margin-right: 10px;
  font-size: 14px;
  color: #bababa;
  background: #fff;
  cursor: pointer;
}
.pay_main .el-row .acceptEmail:hover {
  color: #f87a23;
}
.pay_main .inputTitle {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
}
.pay_main .inputTitle span {
  font-size: 14px;
  color: #999;
  font-weight: 400;
}
::v-deep .pay_main .el-tabs__header {
  border: 0;
  margin-bottom: 24px;
}
::v-deep .pay_main .el-tabs__nav {
  width: 100%;
  display: flex;
  justify-content: space-between;
  border: 0;
}
::v-deep .pay_main .el-tabs__item {
  width: 49%;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  border: 0;
  background-color: #fafafa;
}
::v-deep .pay_main .el-tabs__item.is-active {
  background-color: #f1f3ff;
  color: #39486f;
}
::v-deep .pay_main .el-tabs__item:hover {
  color: #39486f;
}

.askGold {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
}
.askGold li {
  display: flex;
  margin-bottom: 24px;
}
.askGold li .el-avatar {
  margin-right: 14px;
}
.askGold .askInfo h3 {
  margin-bottom: 5px;
  font-size: 16px;
  color: #333;
}
.askGold .askInfo {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #ecedf0;
  padding-bottom: 24px;
}
.askGold .askInfo p {
  font-size: 12px;
  color: #666;
}
.askGold .askInfo button {
  color: #39486f;
  border-color: #39486f;
  padding: 5px 15px;
}
::v-deep .askInfo .el-radio__label {
  display: none;
}
::v-deep .askInfo .el-radio .el-radio__inner {
  width: 24px;
  height: 24px;
  border-color: #bbbbbb;
  background: #fff;
}
::v-deep .askInfo .el-radio .is-disabled .el-radio__inner {
  background: #f2f2f2;
}
::v-deep .askInfo .el-radio .is-checked .el-radio__inner {
  border-color: #f87a23;
  background: #f87a23;
}
::v-deep .askInfo .el-radio__input.is-checked .el-radio__inner::after {
  content: "";
  width: 11px;
  height: 7px;
  border: 3px solid white;
  border-top: transparent;
  border-right: transparent;
  text-align: center;
  display: block;
  position: absolute;
  top: 6px;
  left: 5px;
  vertical-align: middle;
  transform: rotate(-45deg);
  border-radius: 0px;
  background: none;
}
.payBtn {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 9;
  width: 100%;
  background-color: #fff;
  border-top: 1px solid #ecedf0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 30px;
}
.payBtn p {
  margin: 0;
  font-size: 14px;
  font-weight: bold;
}
.payBtn p > span {
  color: #f87a23;
  font-size: 16px;
}
.payBtn .el-button {
  background-color: #f87a23;
  border-color: #f87a23;
  color: #fff;
  font-size: 14px;
  padding: 10px 50px;
}
.payBtn .el-button:hover {
  opacity: 0.9;
}

.scanQr {
  text-align: center;
}
.scanQr img {
  width: 150px;
}
.scanQr p {
  color: #f87a23;
  font-size: 18px;
  font-weight: bold;
}
.scanQr span {
  font-size: 12px;
  color: #999;
}
</style>